package com.threeonemeeting.adapter;

import java.util.ArrayList;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.snda.lib.util.FileHelper;
import com.threeonemeeting.R;
import com.threeonemeeting.model.MeetingDataDocInfo;

public class MeetingDataDocListAdapter extends ArrayAdapter<MeetingDataDocInfo> {

	private ArrayList<MeetingDataDocInfo> mDocItemsList = null;
	
	public void setDocumentList(ArrayList<MeetingDataDocInfo> docList) {
		mDocItemsList = docList;
		notifyDataSetChanged();
	}

	public ArrayList<MeetingDataDocInfo> getDocList() {
		return mDocItemsList;
	}

	@Override
	public void add(MeetingDataDocInfo docInfo) {
		if (mDocItemsList == null)
			mDocItemsList = new ArrayList<MeetingDataDocInfo>();

		mDocItemsList.add(docInfo);
	}

	public void clear() {
		if (mDocItemsList != null) {
			mDocItemsList.clear();
		}
	}

	@Override
	public int getCount() {
		if (mDocItemsList == null)
			return 0;

		return mDocItemsList.size();
	}

	@Override
	public MeetingDataDocInfo getItem(int position) {
		if( position < 0)
			return null;
		
		return mDocItemsList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}
	
	public MeetingDataDocListAdapter(Activity activity) {
		super(activity, 0);
	}

	public View getView(int position, View convertView, ViewGroup parent) {

		Activity activity = (Activity) getContext();
		MeetingDataDocInfo meetingInfoItemData = getItem(position);

		View rowView = convertView;
		MeetingDataDocCache viewCache;
		if (rowView == null) {
			LayoutInflater inflater = activity.getLayoutInflater();
			rowView = inflater
					.inflate(R.layout.lvi_meeting_data_document, null);
			viewCache = new MeetingDataDocCache(rowView,
					meetingInfoItemData.nMeetingId);
			rowView.setTag(viewCache);
		} else {
			viewCache = (MeetingDataDocCache) rowView.getTag();
		}

		// 会议标题
		TextView fileTitleView = viewCache.getMeetingFileTitleTextView();
		String strFileTitle = meetingInfoItemData.strFileName;
		fileTitleView.setText(strFileTitle);

		// 会议图片
		ImageView fileImageView = viewCache.getMeetingImageView();

		if (meetingInfoItemData.strFileType.equalsIgnoreCase("doc") == true) {
			fileImageView.setBackgroundResource(R.drawable.doc);
		} else if (meetingInfoItemData.strFileType
				.equalsIgnoreCase("pdf") == true) {
			fileImageView.setBackgroundResource(R.drawable.pdf);
		} else if (meetingInfoItemData.strFileType
				.equalsIgnoreCase("xls") == true) {
			fileImageView.setBackgroundResource(R.drawable.xls);
		} else if (meetingInfoItemData.strFileType
				.equalsIgnoreCase("zip") == true) {
			fileImageView.setBackgroundResource(R.drawable.zip);
		} else if (meetingInfoItemData.strFileType
				.equalsIgnoreCase("wav") == true) {
			fileImageView.setBackgroundResource(R.drawable.wav);
		} else if (meetingInfoItemData.strFileType
				.equalsIgnoreCase("ppt") == true) {
			fileImageView.setBackgroundResource(R.drawable.ppt);
		} else if (meetingInfoItemData.strFileType
				.equalsIgnoreCase("mp3") == true) {
			fileImageView.setBackgroundResource(R.drawable.mp3);
		} else {
			fileImageView.setBackgroundResource(R.drawable.document);
		}

		// 文件上传时间
		TextView fileTimeView = viewCache.getMeetingFileTimeTextView();
		fileTimeView.setText(meetingInfoItemData.strFileTime);

		// 文件的大小
		TextView fileSizeView = viewCache.getMeetingFileSizeTextView();
		int nSize = meetingInfoItemData.nFileSize;
		String sizeDesc = FileHelper.getSizeDesc(nSize);
		fileSizeView.setText(sizeDesc);

		return rowView;
	}

	
	public class MeetingDataDocCache {

		private View baseView;
		private int meetingId;
		private ImageView meetingDataFileImageView;

		private TextView meetingDataFileTitleTextView;
		private TextView meetingDataFileTimeTextView;
		private TextView meetingDataFileSizeTextView;

		public MeetingDataDocCache(View baseView, int meetingId) {
			this.baseView = baseView;
			this.meetingId = meetingId;
		}

		public int getMeetingId() {
			return this.meetingId;
		}

		public ImageView getMeetingImageView() {
			if (meetingDataFileImageView == null) {
				meetingDataFileImageView = (ImageView) baseView
						.findViewById(R.id.meeting_data_file_image);
			}
			return meetingDataFileImageView;
		}

		public TextView getMeetingFileTitleTextView() {
			if (meetingDataFileTitleTextView == null) {
				meetingDataFileTitleTextView = (TextView) baseView
						.findViewById(R.id.meeting_data_file_title);
			}
			return meetingDataFileTitleTextView;
		}

		public TextView getMeetingFileTimeTextView() {
			if (meetingDataFileTimeTextView == null) {
				meetingDataFileTimeTextView = (TextView) baseView
						.findViewById(R.id.meeting_data_file_time);
			}
			return meetingDataFileTimeTextView;
		}
		
		public TextView getMeetingFileSizeTextView() {
			if (meetingDataFileSizeTextView == null) {
				meetingDataFileSizeTextView = (TextView) baseView
						.findViewById(R.id.meeting_data_file_size);
			}
			return meetingDataFileSizeTextView;
		}
	}
}
